import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
var moment = require('moment');
import { message } from 'antd';
/**
 * 工具组件
 * @Author: cly
 * @Date: 2018-10-11 11:43:25
 */

var Utils = function () {
    function Utils() {
        _classCallCheck(this, Utils);
    }

    _createClass(Utils, null, [{
        key: 'phoneHide',

        /**
         * 手机号脱敏
         * @param origin 源数据
         */
        value: function phoneHide(origin) {
            // return origin.substr(0, 3) + '****' + origin.substr(7, 11);
            return origin.slice(0, 3) + " **** " + origin.slice(-4);
        }
        /**
         * 身份证号脱敏
         * @param origin 源数据
         */

    }, {
        key: 'certNoHide',
        value: function certNoHide(origin) {
            if (!origin) {
                return;
            } else if (origin.length !== 18) {
                return;
            } else {
                return origin.slice(0, 5) + "***********" + origin.slice(-2);
            }
        }
        /**
         * 姓名脱敏
         * @param origin 源数据
         */

    }, {
        key: 'nameHide',
        value: function nameHide(origin) {
            if (!origin) {
                return origin;
            }
            if (origin.length < 1) {
                return origin;
            } else {
                return "*" + origin.slice(-1);
            }
        }
        /**
         * 卡号增加空格
         * @param origin 源数据
         */

    }, {
        key: 'cardNoHandle',
        value: function cardNoHandle(origin) {
            if (origin === undefined) {
                return origin;
            }
            var cardNoVal = origin.slice(0, 4) + " " + origin.slice(4, 6);
            for (var i = 6; i < origin.length - 4; i++) {
                if (i % 4 == 0) {
                    cardNoVal = cardNoVal + " ";
                }
                cardNoVal = cardNoVal + "*";
            }
            cardNoVal = cardNoVal + " ";
            cardNoVal = cardNoVal + origin.slice(-4);
            return cardNoVal;
        }
        /**
         * 数值货币化,向下取整
         * @param origin 源数据
         */

    }, {
        key: 'currencyFormat',
        value: function currencyFormat(origin) {
            if (origin) {
                origin = origin.toString().replace(/\$|\,/g, '');
                if (isNaN(origin)) {
                    return '0';
                }
                var sign = origin == (origin = Math.abs(origin));
                origin = Math.round(origin * 100);
                var cents = origin % 100;
                origin = Math.floor(origin / 100).toString();
                if (cents < 10) cents = "0" + cents;
                for (var i = 0; i < Math.floor((origin.length - (1 + i)) / 3); i++) {
                    origin = origin.substring(0, origin.length - (4 * i + 3)) + ',' + origin.substring(origin.length - (4 * i + 3));
                }return (sign ? '' : '-') + origin + '.' + cents;
            } else if (origin === 0) {
                return '0';
            } else {
                return '';
            }
        }
        /**
        * 时间格式化
        */

    }, {
        key: 'timeFormat',
        value: function timeFormat(time, format) {
            if (!time) {
                return '';
            }
            if (time instanceof String && time.indexOf('T') > -1) {
                time = new Date(time.replace('T', ' ').split('.')[0]).getTime();
            }
            return moment(time).format(format);
            ;
        }
        /**
        * 按指定位数截取数字,不进行四舍五入
        * @param origin count  源数据
        */

    }, {
        key: 'numCut',
        value: function numCut(origin, count) {
            if (!origin) {
                return 0;
            }
            return Math.floor(origin * Math.pow(10, count)) / Math.pow(10, count);
        }
        /**
         * 按指定位数截取数字,进行四舍五入
         * @param origin 源数据
         */

    }, {
        key: 'numRoundCut',
        value: function numRoundCut(origin, count) {
            if (!origin) {
                return 0;
            }
            return Math.round(origin * Math.pow(10, count)) / Math.pow(10, count);
        }
        /**
        * 复制
        * @param text 文本
        */

    }, {
        key: 'copy',
        value: function copy(text) {
            var input = document.createElement('input');
            try {
                input.value = text;
                document.body.appendChild(input);
                input.select();
                document.execCommand('copy');
                message.success('成功复制到剪贴板');
                input.remove();
            } catch (err) {
                message.error('该浏览器不支持复制功能，请使用Ctrl C进行复制');
            }
        }
        /**
         * 今天
         * @return 今天的时间区间
         * @param format1 开始时间格式化, 默认YYYY-MM-DD 00:00:00
         * @param format2 结束时间格式化, 默认YYYY-MM-DD 23:59:59
         */

    }, {
        key: 'thisDay',
        value: function thisDay() {
            var format1 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'YYYY-MM-DD 00:00:00';
            var format2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'YYYY-MM-DD 23:59:59';

            return {
                start: moment().format(format1),
                end: moment().format(format2)
            };
        }
        /**
         * 本周
         * @return 本周的时间区间
         * @param format1 开始时间格式化, 默认YYYY-MM-DD 00:00:00
         * @param format2 结束时间格式化, 默认YYYY-MM-DD 23:59:59
         */

    }, {
        key: 'thisWeek',
        value: function thisWeek() {
            var format1 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'YYYY-MM-DD 00:00:00';
            var format2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'YYYY-MM-DD 23:59:59';

            return {
                start: moment().week(moment().week()).startOf('week').format(format1),
                end: moment().week(moment().week()).endOf('week').format(format2)
            };
        }
        /**
         * 本月
         * @return 本月的时间区间
         * @param format1 开始时间格式化, 默认YYYY-MM-DD 00:00:00
         * @param format2 结束时间格式化, 默认YYYY-MM-DD 23:59:59
         */

    }, {
        key: 'thisMonth',
        value: function thisMonth() {
            var format1 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'YYYY-MM-DD 00:00:00';
            var format2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'YYYY-MM-DD 23:59:59';

            return {
                start: moment().month(moment().month()).startOf('month').format(format1),
                end: moment().month(moment().month()).endOf('month').format(format2)
            };
        }
        /**
         * 金额转大写
         * @param num 金额
         */

    }, {
        key: 'moneyToCapital',
        value: function moneyToCapital(num) {
            if (num === "" || num === undefined || num === null) {
                return "";
            }
            var fuhao = "";
            var text = num + "";
            if (text.indexOf("-") > -1) {
                num = text.replace("-", "");
                fuhao = "负";
            }
            if (num === "0" || num === "0.0" || num === "0.00") {
                return "零元整";
            }
            var money1 = parseFloat(num);
            var monee = Math.round(money1 * 100).toString(10);
            var leng = monee.length;
            var monval = "";
            for (var i = 0; i < leng; i++) {
                monval = monval + this.to_upper(monee.charAt(i)) + this.to_mon(leng - i - 1);
            }
            return fuhao + this.repace_acc(monval);
        }
        // 将数字转为大写的中文字

    }, {
        key: 'to_upper',
        value: function to_upper(a) {
            switch (a) {
                case "0":
                    return "零";
                    break;
                case "1":
                    return "壹";
                    break;
                case "2":
                    return "贰";
                    break;
                case "3":
                    return "叁";
                    break;
                case "4":
                    return "肆";
                    break;
                case "5":
                    return "伍";
                    break;
                case "6":
                    return "陆";
                    break;
                case "7":
                    return "柒";
                    break;
                case "8":
                    return "捌";
                    break;
                case "9":
                    return "玖";
                    break;
                default:
                    return "";
            }
        }
    }, {
        key: 'to_mon',
        value: function to_mon(a) {
            if (a > 10) {
                a = a - 8;
                return this.to_mon(a);
            }
            switch (a) {
                case 0:
                    return "分";
                    break;
                case 1:
                    return "角";
                    break;
                case 2:
                    return "元";
                    break;
                case 3:
                    return "拾";
                    break;
                case 4:
                    return "佰";
                    break;
                case 5:
                    return "仟";
                    break;
                case 6:
                    return "万";
                    break;
                case 7:
                    return "拾";
                    break;
                case 8:
                    return "佰";
                    break;
                case 9:
                    return "仟";
                    break;
                case 10:
                    return "亿";
                    break;
            }
        }
    }, {
        key: 'repace_acc',
        value: function repace_acc(Money) {
            Money = Money.replace("零分", "");
            Money = Money.replace("零角", "零");
            var yy;
            var outmoney;
            outmoney = Money;
            yy = 0;
            while (true) {
                var lett = outmoney.length;
                outmoney = outmoney.replace("零元", "元");
                outmoney = outmoney.replace("零万", "万");
                outmoney = outmoney.replace("零亿", "亿");
                outmoney = outmoney.replace("零仟", "零");
                outmoney = outmoney.replace("零佰", "零");
                outmoney = outmoney.replace("零零", "零");
                outmoney = outmoney.replace("零拾", "零");
                outmoney = outmoney.replace("亿万", "亿零");
                outmoney = outmoney.replace("万仟", "万零");
                outmoney = outmoney.replace("仟佰", "仟零");
                yy = outmoney.length;
                if (yy == lett) {
                    break;
                }
            }
            yy = outmoney.length;
            if (outmoney.charAt(yy - 1) == "零") {
                outmoney = outmoney.substring(0, yy - 1);
            }
            yy = outmoney.length;
            if (outmoney.charAt(yy - 1) == "元") {
                outmoney = outmoney + "整";
            }
            return outmoney;
        }
    }]);

    return Utils;
}();

export default Utils;